<?php

namespace app\common\service;

use app\admin\Service;
use AopClient;
include_once(public_path() . '/../extend/alipayphpsdk/aop/AopClient.php');
use AlipayTradeWapPayRequest;
include_once(public_path() . '/../extend/alipayphpsdk/aop/request/AlipayTradeWapPayRequest.php');

class AlipayService
{

    protected $rsaPrivateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC2HkRNODIRK0IGtv+KFLVl8/JRW9z7tQphwYqlMtK1js6/pEHOyNgcXPN7jeo6H/p16Cz17J+EsRwRDwbyjA458UDnlekGMu8e3UgYDuJ+bXGJMdiezdmoa7T8RDm9D0WB0pb75nhClBHq2JXqxJ+tF1vkdPt0gNnfO77azilzkTFAjCgrlhL8hN/3VcwBVXXFTdNtfuVSdPYX8NHlvJmJxMlff3jACR5vMoNXI/ayZRO7DPsh5XVPh8vEjDqiOV1aaeoIIrfST+1Uw0KBfh9naPjKIjnf4w326uvUKu1y54Db+KZm3C430zMq5JCxQ/IF6vQnZXnRaQ2re1O2i87fAgMBAAECggEAXVj2OX5sa1B472gtY2Fp+XAo7HrhskMBp0nE/xgl2NnyL1jKQmvAGgmq49HCFTj+8CCpqwQ2X5VIhZXmQbx4LkryscGtR3gkCPXzViGLBrKiV2gtWHi68BEFG6j8z2NchUakDUn8qmOrK20IquVN0zL97wPnP7gr9LhzA0RZCtqpQD6FGSGGjxJeZKM5j/sbeTzxQwIyO/TZM44WEkW1SHsCjtAIozWsYym7sa7NghPUowU/P7uKef6WVtY4Hv6nLQ+xD5ovJYM/sRBgEgsxNSuempcKUNh4L3dlDBRoKio8/IfkQhNCLz6casWoxPzoeOEdANuLkE/U210A/l3EQQKBgQD69aqrh0il9fjmOcbzejJGdf8Oq+GMovhw+AxgF08dqzMJ2Je4S1sCuEdrdfksbikipfzl5SWOgHd/PUYU1KcwYiIuFfsQHAWSJoXnKKxs/f3e59NIJZZ29OvxfQQGwObmpDATKymC2a7W9UoBWk/7Nv4yw5WFtT3tBijvLnjQGwKBgQC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IgAMAK1nZslXxe0C1PHHeYnZwQIDAQAB';

    public function h5Pay($params = [])
    {
        $aop = new AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId =  '2021004148694141';
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey =  $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';

        $object = new \stdClass();
        $object->out_trade_no   = $params['out_trade_no'];
        $object->total_amount   = $params['total_amount'];
        //$object->total_amount   = 0.01;
        $object->subject        = $params['subject'];
        $object->product_code       = "QUICK_WAP_WAY";

        $json = json_encode($object);

        $request = new AlipayTradeWapPayRequest();

        $request -> setNotifyUrl(env('APP_HOST')."api/callback/payH5Notify");

        $request -> setBizContent($json);
        //halt($request);

        $pageRedirectionData = $aop->pageExecute($request,"GET");

        return $pageRedirectionData;
    }
}